package org.crud4j.validator;

import java.lang.annotation.Annotation;

/**
 * <p>
 * {@link ValueValidator} wrapper that delegates the call to an underlying
 * {@link ValueValidator} only if the given value is not null. <br>
 * The purpose of this class is to remove the burden of handling null values in
 * {@link ValueValidator} implementations.
 * </p>
 */
public class NullAwareBeanValidatorWrapper implements ValueValidator {

	private ValueValidator delegatee;

	public NullAwareBeanValidatorWrapper(ValueValidator delegatee) {
		super();
		this.delegatee = delegatee;
	}

	@SuppressWarnings("unchecked")
	public Class getAnnotationClass() {
		return delegatee.getAnnotationClass();
	}

	public void init(Annotation annotation) {
		delegatee.init(annotation);
	}

	public void validate(Object value) throws ValueValidatorException {
		if (value != null) {
			this.delegatee.validate(value);
		}
	}

}
